/************************************************************************/
/* fxdecoder.cpp                                                        */
/************************************************************************/

#include        <fstream>
#include        <vector>
#include        <map>
#include        <string>

/************************************************************************/
// fxresource

#include        <d3dx9.h>
#include        "FXDynamicArray.h"
#include        "FXDataStruct.h"
#include        "FXEvent.h"
#include        "FXSeq.h"
#include        "fxresource.h"

/************************************************************************/

#include        "fxeventdecoder.h"

using namespace     FX;

/************************************************************************/
/*                                                                      */
/************************************************************************/
void
CFXEventDecoder::
OpenEventScriptFile(std::string strEventScriptFileName,
                    EVENT_VECTOR_MAP& eventVectorMap)
{
    ifstream        inputFileStream;
    inputFileStream.open(strEventScriptFileName.c_str());

    if (!inputFileStream.is_open()) { return; }
    
    try
    {
        DecodeEventScript(inputFileStream, eventVectorMap);
    }
    catch(char* szExceptionLog)
    {
        inputFileStream.close();
        
        strEventScriptFileName += " ";
        strEventScriptFileName += szExceptionLog;
        strEventScriptFileName += "\n";
        OutputDebugString(strEventScriptFileName.c_str());

        return;
    }
    catch (...)
    {
        inputFileStream.close();

        strEventScriptFileName += " ";
        strEventScriptFileName += "Unknown exception occurred.";
        strEventScriptFileName += "\n";
        OutputDebugString(strEventScriptFileName.c_str());

        return;
    }
}

/************************************************************************/
/*                                                                      */
/************************************************************************/
void
CFXEventDecoder::
DecodeEventScript(ifstream& inputFileStream,
                  EVENT_VECTOR_MAP& eventVectorMap)
{

    // fx_event_script v0.0
    // name test
    // event texture 0.4 torch_0004.dxt
    // end


    char        szBuffer[256] = "";    
    char        szReadFromBuffer[32] = ""; 
    
    inputFileStream.getline(szBuffer, sizeof(szBuffer));
    sscanf(szBuffer, "%s", szReadFromBuffer);


    EVENT_VECTOR*       pEventVectorCreated = 0;
    char                szEventVectorName[56] = ""; 

    try
    {
        if (_stricmp(szReadFromBuffer, "fx_event_script"))
        {
            throw "Invalid data";
        }
        
        inputFileStream.getline(szBuffer, sizeof(szBuffer));

        while (!inputFileStream.eof())
        {
            if (!_stricmp(szReadFromBuffer, "name"))
            {
                sscanf(szBuffer, "%s%s", szReadFromBuffer, szEventVectorName);
                pEventVectorCreated = new EVENT_VECTOR();
            }
            if (!_stricmp(szReadFromBuffer, "event"))
            {
                pEventVectorCreated->push_back(CreateEvent(szBuffer));
            }

            inputFileStream.getline(szBuffer, sizeof(szBuffer));
            sscanf(szBuffer, "%s", szReadFromBuffer);
        }
    }
    catch (char* szExceptionLog)
    {
        DeleteEventVector(*pEventVectorCreated);
        throw szExceptionLog;
    }
    catch (...) { DeleteEventVector(*pEventVectorCreated); throw; }
    
    if (!pEventVectorCreated->size()) { delete pEventVectorCreated; }
    else
    {
        AddEventVectorToMap(
            eventVectorMap, szEventVectorName, *pEventVectorCreated);
    }

}

/************************************************************************/
/* ?                                                                    */
/************************************************************************/
void
CFXEventDecoder::
DeleteEventVector(FXEVENT_VECTOR& eventVector)
{
    for (EVENT_ITER iter = eventVector.begin();
    iter != eventVector.end(); ++iter)
    {
        delete *iter;
    }
    eventVector.clear();
}

/************************************************************************/
/*                                                                      */
/************************************************************************/
void
CFXEventDecoder::
AddEventVectorToMap(EVENT_VECTOR_MAP& eventVectorMap,
                    std::string strEventVectorName,
                    FXEVENT_VECTOR& eventVector)
{
    EVENT_VECTOR_PAIR       eventVectorPair;

    eventVectorPair.first = strEventVectorName;
    eventVectorPair.second = &eventVector;
    eventVectorMap.insert(eventVectorPair);
}

/************************************************************************/
/*                                                                      */
/************************************************************************/
IFXEvent*
CFXEventDecoder::
CreateEvent(char* szLineRead)
{
    char        szReadFromLine[32];
    char        szEvent[32];

    sscanf(szLineRead, "%s%s", szReadFromLine, szEvent);
    if (!_stricmp(szEvent, "texture"))
    {
        return CreateTextureChangeEvent(szLineRead);
    }
    else if (!_stricmp(szEvent, "color"))
    {
        return CreateColorEvent(szLineRead);
    }
    else if (!_stricmp(szEvent, "scale"))
    {
        return CreateScaleEvent(szLineRead);
    }
    else
    {
        throw "invalid event";
    }
    return 0;
}


/************************************************************************/
/* ?                                                                    */
/************************************************************************/
IFXEvent*
CFXEventDecoder::
CreateTextureChangeEvent(char* szLineRead)
{
    return 0;
}

/************************************************************************/
/* ?                                                                    */
/************************************************************************/
IFXEvent*
CFXEventDecoder::
CreateColorEvent(char* szLineRead)
{
    return 0;
}

/************************************************************************/
/* ?                                                                    */
/************************************************************************/
IFXEvent*
CFXEventDecoder::
CreateScaleEvent(char* szLineRead)
{
    return 0;
}